package md.mru.core.service;

import md.mru.core.common.BaseDtoMapper;
import md.mru.core.domains.Folder;
import md.mru.core.domains.FolderState;
import md.mru.core.domains.type.OrderStateType;
import md.mru.core.dtos.FolderDto;
import md.mru.core.dtos.FolderStateDto;
import md.mru.core.dtos.OrderDto;
import md.mru.core.repositories.FolderRepository;
import md.mru.core.repositories.FolderStateRepository;
import md.mru.frontend.utils.FolderStateType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 01.04.2014
 */
//@Transactional
@Service
public class FolderService {

    @Autowired
    @Qualifier(value = "dtoMapper")
    private BaseDtoMapper mapper;
    @Autowired
    private FolderRepository folderRepository;
    @Autowired
    private FolderStateRepository folderStateRepository;


    public long save(FolderDto folderDto) {
        Folder folder = mapper.map(folderDto, Folder.class);
        return folderRepository.save(folder).getId();
    }

    public List<FolderDto> getFolders() {
        List<Folder> folders = folderRepository.findAll();
        return mapper.mapList(folders, FolderDto.class);
    }

    public List<FolderDto> getUnAtachedFolders() {
        List<Folder> folders = folderRepository.getUnAttachedFolders(FolderStateType.NEW.folderStateId);
        return mapper.mapList(folders, FolderDto.class);
    }

    public FolderDto getFolder(long folderId) {
        Folder folder = folderRepository.findOne(folderId);
        return mapper.map(folder, FolderDto.class);
    }

    public void save(long folderId) {
        //this.folderRepository.save(folderId, generalState, currentState);
    }

    public List<FolderDto> loadFoldersByStates(EnumSet<OrderStateType> orderStates) {
        List<Folder> folders = folderRepository.loadFoldersByStates(orderStates);
        return mapper.mapList(folders, FolderDto.class);
    }

    public List<FolderDto> getUnAssociatedFolders() {
        List<Folder> folders = folderRepository.loadUnAssociatedFolders();
        return mapper.mapList(folders, FolderDto.class);
    }

    public FolderStateDto getFolderState(long folderStateId) {
        FolderState folderState = folderStateRepository.loadFolderState(folderStateId);
        return mapper.map(folderState, FolderStateDto.class);
    }

    public List<FolderDto> loadFoldersByState(Long... folderStateIds) {
        List<Folder> folders = folderRepository.loadFoldersByState(new HashSet<>(Arrays.asList(folderStateIds)));
        return mapper.mapList(folders, FolderDto.class);
    }

    @Transactional
    public void changeFolderStates(List<OrderDto> orders) {
        for (OrderDto order : orders) {
            for (FolderDto folder : order.getFolders()) {
                folderRepository.updateFolderState(order.getOrderType().getChangeToOrderTypeId(), folder.getId());
            }
        }
    }

    public byte[] loadFolderImage(long folderId) {
        return null;
    }
}
